package cn.newgrand.netcall.search.contact;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JLabel;

import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;

import cn.newgrand.netcall.base.ContactManager;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-8-2 下午01:52:57 类说明
 */
public class ContactSearch {

	private Map<JLabel, String> jidMap = new HashMap<JLabel, String>();
	private Collection<ContactSearchBean> allContacts ;
	private HashMap<String,ContactSearchBean> searchResults ;
	private List<ContactSearchBean> resultBeanList  = new ArrayList<ContactSearchBean>();
	private List<String> recentContacts ;
	char[] chars;

	
	/**
	 * 正则表达式方式查找用户
	 * 
	 * @param str
	 * @return 返回map
	 */
	public HashMap<String,ContactSearchBean> searchContacts(String str){
		searchResults = new HashMap<String,ContactSearchBean>();
		if(str.equals("")||null==str){return searchResults;}
		str = str.trim();
		str = str.toLowerCase();
		allContacts = SparkManager.getContactManager().getAllContactsPY().values();
		//如果输入的字符串中含有字母、数字、汉字、以外的字符则返回为空
		String regex = "[^\\w\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		if(m.matches()){
			return searchResults;
		}
		
		StringBuffer sb = new StringBuffer(".*");
		sb.append(str);
		sb.append(".*");
		String regexStr = sb.toString();
		
		for (ContactSearchBean csb : allContacts) {
			if(null!=csb.getNamePYF()&&csb.getNamePYF().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
			if(null!=csb.getNamePY()&&csb.getNamePY().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
			if(null!=csb.getNickNamePY()&&csb.getNickNamePY().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
			if(null!=csb.getNickNamePYF()&&csb.getNickNamePYF().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
			if(null!=csb.getNickName()&&csb.getNickName().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
			if(null!=csb.getName()&&csb.getName().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
			
			if(null!=csb.getUserName()&&csb.getUserName().matches(regexStr)) {
				searchResults.put(csb.getJID(),csb);
				continue;
			}
		}
		return searchResults;
	}
	
	
	/**
	 * 获取联系人的JID形式存储
	 * 
	 * @param str 查询的字符串
	 * @return
	 */
	public List<String> getContactsJID(String str){
		ArrayList<String> resultStrListTemp = new ArrayList<String>();
		for (ContactSearchBean csb : searchContacts(str).values()) {
			resultStrListTemp.add(csb.getJID());
		}
		return sortContactsByChatedTime(resultStrListTemp);
	}
	
	/**
	 * 
	 * 根据最后聊天时间排序联系人
	 * 
	 * @param results
	 * @return
	 */
	public  List<String> sortContactsByChatedTime(ArrayList<String> results){
		if(results==null) return null;
		recentContacts = ContactManager.getInstance().getRecentContacts();
		ArrayList<String> resultsTemp = new ArrayList<String>();
		Object[] strs = results.toArray();
		for (String JID : recentContacts) {
			if(results.contains(JID)){
				results.remove(JID);
				resultsTemp.add(JID);
			}
		}
		resultsTemp.addAll(results);
		return resultsTemp;
	}
	
}
